package com.bw.mallshop;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.bw.goods.mvvm.view.fragment.HomeGoodsFragment;
import com.bw.mallshop.mvvm.viewmodel.BottomViewModel;
import com.bw.mallshop.widegt.BottomButton;
import com.bw.mvvm_common.mvvm.view.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class MainActivity extends AppCompatActivity implements BottomButton.BottomSelectListener{


    Fragment goodsFragment;
    Fragment changeFragment;
    Fragment userFragment;
    Fragment shoppFragment;
    Fragment msgFragment;
    private BottomButton frameHomeBtn;
    private BottomButton frameClassificationBtn;
    private BottomButton frameShoppingBtn;
    private BottomButton frameNewsBtn;
    private BottomButton frameMineBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
            goodsFragment =(Fragment) ARouter.getInstance()
                    .build("/com/bw/goods/mvvm/view/fragment/HomeGoodsFragment").navigation();
            ft.add(R.id.main_fg_group,goodsFragment).commit();

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 23) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }




    @Override
    public void onSelect(int id) {
        clearBottomSel();
        hideFragment();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        switch (id) {
            case R.id.frame_home_btn:
                frameHomeBtn.setSelFlag(true);
                if (goodsFragment==null){
                    goodsFragment = (Fragment) ARouter.getInstance()
                            .build("/com/bw/goods/mvvm/view/fragment/HomeGoodsFragment").navigation();
                    ft.add(R.id.main_fg_group, goodsFragment);
                }else {
                    ft.show(goodsFragment);
                }
                break;
            case R.id.frame_classification_btn:
                frameClassificationBtn.setSelFlag(true);
                if (changeFragment==null){
                    changeFragment = (Fragment) ARouter.getInstance()
                            .build("/com/bw/goods/mvvm/view/fragment/ChanageFragment").navigation();
                    ft.add(R.id.main_fg_group, changeFragment);
                }else {
                    ft.show(changeFragment);
                }
                break;
            case R.id.frame_shopping_btn:
                frameShoppingBtn.setSelFlag(true);
                if (shoppFragment==null){
                    shoppFragment = (Fragment) ARouter.getInstance()
                            .build("/kkshopping/ShoppFragment").navigation();
                    ft.add(R.id.main_fg_group, shoppFragment);
                }else {
                    ft.show(shoppFragment);
                }
                break;
            case R.id.frame_news_btn:
                frameNewsBtn.setSelFlag(true);
                if (msgFragment==null){
                    msgFragment = (Fragment) ARouter.getInstance()
                            .build("/kkmsg/UserFragment").navigation();
                    ft.add(R.id.main_fg_group, msgFragment);
                }else {
                    ft.show(msgFragment);
                }
                break;
            case R.id.frame_mine_btn:
                frameMineBtn.setSelFlag(true);
                if (userFragment==null){
                    userFragment = (Fragment) ARouter.getInstance()
                            .build("/kkuser/UserFragment").navigation();
                    ft.add(R.id.main_fg_group, userFragment);
                }else {
                    ft.show(userFragment);
                }
                break;
        }
        ft.commit();
    }

    private void hideFragment() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        if (goodsFragment != null) {
            ft.hide(goodsFragment);
        }
        if (changeFragment != null) {
            ft.hide(changeFragment);
        }
        if (shoppFragment != null) {
            ft.hide(shoppFragment);
        }
        if (msgFragment != null) {
            ft.hide(msgFragment);
        }
        if (userFragment != null) {
            ft.hide(userFragment);
        }
        ft.commit();
    }

    //清理底部的选中效果
    private void clearBottomSel() {
        frameHomeBtn.setSelFlag(false);
        frameClassificationBtn.setSelFlag(false);
        frameShoppingBtn.setSelFlag(false);
        frameNewsBtn.setSelFlag(false);
        frameMineBtn.setSelFlag(false);
    }

    private void initView() {
        frameHomeBtn = findViewById(R.id.frame_home_btn);
        frameClassificationBtn = findViewById(R.id.frame_classification_btn);
        frameShoppingBtn = findViewById(R.id.frame_shopping_btn);
        frameNewsBtn = findViewById(R.id.frame_news_btn);
        frameMineBtn = findViewById(R.id.frame_mine_btn);
        frameHomeBtn.setContent("首页")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.ic_diannao)
                .setSelImg(R.drawable.ic_diannao)
                .setNamalSize(5)
                .setSelSize(7)
                .setListener(this)
                .refresh();
        frameClassificationBtn.setContent("分类")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.ic_diannao)
                .setSelImg(R.drawable.ic_diannao)
                .setNamalSize(5)
                .setSelSize(7)
                .setListener(this)
                .refresh();
        frameShoppingBtn.setContent("购物车")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.ic_diannao)
                .setSelImg(R.drawable.ic_diannao)
                .setNamalSize(5)
                .setSelSize(7)
                .setListener(this)
                .refresh();
        frameNewsBtn.setContent("消息")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.ic_diannao)
                .setSelImg(R.drawable.ic_diannao)
                .setNamalSize(5)
                .setSelSize(7)
                .setListener(this)
                .refresh();
        frameMineBtn.setContent("我的")
                .setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.ic_diannao)
                .setSelImg(R.drawable.ic_diannao)
                .setNamalSize(5)
                .setSelSize(7)
                .setListener(this)
                .refresh();
    }
}
